package com.kungfuhacking.wristbandpro.chat.presenter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.Mapplication;
import com.kungfuhacking.wristbandpro.chat.bean.EmoBean;
import com.kungfuhacking.wristbandpro.mqtt.MqttConstant;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


/**
 * Created by Aaron on 15/12/28.
 */
public class EmoGrideAdapter extends BaseAdapter {

    private Context ctx;

    private int emoPageNum;

    private Mapplication mApplication;

    private int[] emos_o = {R.drawable.expression_12x, R.drawable.expression_22x, R.drawable.expression_32x, R.drawable.expression_42x,
            R.drawable.expression_52x, R.drawable.expression_62x, R.drawable.expression_72x, R.drawable.expression_82x,
            R.drawable.expression_92x, R.drawable.expression_102x, R.drawable.expression_112x, R.drawable.expression_122x,
            R.drawable.expression_132x, R.drawable.expression_142x, R.drawable.expression_152x, R.drawable.expression_162x,
            R.drawable.expression_172x, R.drawable.expression_182x, R.drawable.expression_192x, R.drawable.expression_202x,
            R.drawable.delete_x};

    private int[] emos_s = {R.drawable.expression_212x, R.drawable.expression_222x, R.drawable.expression_232x, R.drawable.expression_242x,
            R.drawable.expression_252x, R.drawable.expression_262x, R.drawable.expression_272x, R.drawable.expression_282x,
            R.drawable.expression_292x, R.drawable.expression_302x, R.drawable.expression_312x, R.drawable.expression_322x,
            R.drawable.expression_332x, R.drawable.expression_342x, R.drawable.expression_352x, R.drawable.expression_362x,
            R.drawable.expression_372x, R.drawable.expression_382x, R.drawable.expression_392x, R.drawable.expression_402x,
            R.drawable.delete_x};

    private int[] emos_t = {R.drawable.expression_412x, R.drawable.expression_422x, R.drawable.expression_432x, R.drawable.expression_442x,
            R.drawable.expression_452x, R.drawable.expression_462x, R.drawable.expression_472x, R.drawable.expression_482x,
            R.drawable.expression_492x, R.drawable.expression_502x, R.drawable.expression_512x, R.drawable.expression_522x,
            R.drawable.expression_532x, R.drawable.expression_542x, R.drawable.expression_552x, R.drawable.expression_562x,
            R.drawable.expression_572x, R.drawable.expression_582x, R.drawable.expression_592x, R.drawable.expression_602x,
            R.drawable.delete_x};


    public EmoGrideAdapter(Context ctx, int pageNum) {
        this.ctx = ctx;
        this.emoPageNum = pageNum;
        this.mApplication = (Mapplication) ctx.getApplicationContext();
    }

    @Override
    public int getCount() {
        return emos_o.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = new ImageView(ctx);
            viewHolder = new ViewHolder();

            ViewGroup.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            convertView.setLayoutParams(params);
            viewHolder.iv_emo = (ImageView) convertView;
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        switch (emoPageNum) {

            case MqttConstant.EMO_PAGE_ONE:
                viewHolder.iv_emo.setImageResource(emos_o[position]);
                break;

            case MqttConstant.EMO_PAGE_TWO:
                viewHolder.iv_emo.setImageResource(emos_s[position]);
                break;

            case MqttConstant.EMO_PAGE_THREE:
                viewHolder.iv_emo.setImageResource(emos_t[position]);
                break;

        }

        viewHolder.iv_emo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                LogUtil.e("tag", "点击的图标:" + );

                //判断是否是最后一个的删除图标
                if (position == emos_s.length - 1) {
                    SpannableString s = new SpannableString(MqttConstant.DELETEEMO);
                    //通知聊天页面 删除图标
                    EventBus.getDefault().post(s);
                    return;
                }

                switch (emoPageNum) {

                    case MqttConstant.EMO_PAGE_ONE:

                        EventBus.getDefault().post(getSelectImage(ctx, emos_o[position], position));

//                        LogUtil.e("tag", "点击的图标:" + emos_o[position] + "EMO_PAGE_ONE");
                        break;

                    case MqttConstant.EMO_PAGE_TWO:
                        EventBus.getDefault().post(getSelectImage(ctx, emos_s[position], position + 20));
//                        LogUtil.e("tag", "点击的图标:" + emos_s[position] + "EMO_PAGE_TWO");
                        break;

                    case MqttConstant.EMO_PAGE_THREE:
                        EventBus.getDefault().post(getSelectImage(ctx, emos_t[position], position + 40));
//                        LogUtil.e("tag", "点击的图标:" + emos_t[position] + "EMO_PAGE_THREE");
                        break;

                }
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private ImageView iv_emo;
    }


    /**
     * 设置可在edittext里显示的图片
     *
     * @param ctx
     * @param imageId
     * @param position 点击的表情的位置
     * @return
     */
    private SpannableString getSelectImage(Context ctx, int imageId, int position) {

        try {
//            //  根据资源ID获得资源图像的Bitmap对象
//            Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(), imageId);
//            //  根据Bitmap对象创建ImageSpan对象
//            ImageSpan imageSpan = new ImageSpan(ctx, bitmap);

            Drawable d = ctx.getResources().getDrawable(imageId);
            d.setBounds(0, 0, 80, 80);//设置表情图片的显示大小
            ImageSpan imageSpan = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
            //  创建一个SpannableString对象，以便插入用ImageSpan对象封装的图像

//            MyApplication mApplication = (MyApplication) ctx.getApplicationContext();
//            HashMap<String, String> emoMap = myApplication.getEmoMap();
            List<EmoBean> emoList = mApplication.getEmoList();

            Log.d(TAG, "选中的表情:" + emoList.get(position).code);
            SpannableString spannableString = new SpannableString(emoList.get(position).code);//参数为图片的正则 (设定规则)
            //  用ImageSpan对象替换f
            spannableString.setSpan(imageSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannableString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    private String TAG = getClass().getSimpleName();
}
